namespace PickGold.Charting.Design
{
    using System;
    using System.ComponentModel;
    using System.Drawing.Design;
    using System.Windows.Forms.Design;

    internal class SeriesDataSourceMemberValueAxisUITypeEditor : UITypeEditor
    {
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (((context != null) && (context.Instance != null)) && (provider != null))
            {
                IWindowsFormsEditorService service = (IWindowsFormsEditorService) provider.GetService(typeof(IWindowsFormsEditorService));
                if (service != null)
                {
                    Chart chartFromContext = ConverterHelper.GetChartFromContext(context);
                    if (chartFromContext != null)
                    {
                        SeriesDataSourceMemberYCheckedListBox control = this.GetDropDownControl(chartFromContext, context, value, true);
                        service.DropDownControl(control);
                        value = control.GetNewValue();
                    }
                }
            }
            return value;
        }

        internal virtual SeriesDataSourceMemberYCheckedListBox GetDropDownControl(Chart chart, ITypeDescriptorContext context, object value, bool flag)
        {
            return new SeriesDataSourceMemberYCheckedListBox(chart, value, flag);
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if ((context == null) || (context.Instance == null))
            {
                return base.GetEditStyle(context);
            }
            int yValuesPerPoint = 1;
            if (context.Instance is Series)
            {
                yValuesPerPoint = ((Series) context.Instance).YValuesPerPoint;
            }
            else if (context.Instance is Array)
            {
                Array instance = (Array) context.Instance;
                if ((instance.Length > 0) && (instance.GetValue(0) is Series))
                {
                    yValuesPerPoint = Math.Max(yValuesPerPoint, ((Series) instance.GetValue(0)).YValuesPerPoint);
                }
            }
            if (yValuesPerPoint != 1)
            {
                return UITypeEditorEditStyle.DropDown;
            }
            return UITypeEditorEditStyle.None;
        }
    }
}

